# -*- mode:Makefile -*-
include ../Makefile.common
LANG=cffi
BASEDIR=..
LMODULE=black-pine
CXXFLAGS+=-I$(BASEDIR)
DISTCLEANOBJ+=
CLEANOBJ+=$(DISTCLEANOBJ) $(LMODULE)*.lisp $(LMODULE)-$(VER).tar.gz $(LMODULE).asd
TARGET=$(LMODULE).so
LISP=sbcl

do:$(TARGET) asdf
$(TARGET): $(MODULE)_wrap.o $(BASEDIR)/$(MODULE).o $(BASEDIR)/$(MODULE)Plugin.o
	$(LINK.cc) -shared -o $@ $^
$(MODULE)_wrap.o: $(MODULE)_wrap.cpp
	$(CXX) $(CXXFLAGS) -c $<
$(MODULE)_wrap.cpp: $(BASEDIR)/$(MODULE).i
	$(SWIG) -c++ -$(LANG) -module $(LMODULE)  -outdir .  -o $@ $(BASEDIR)/$(MODULE).i
	echo "(in-package :${LMODULE})" > $(LMODULE).tmp
	cat $(LMODULE).tmp $(LMODULE).lisp > $(LMODULE).lisp.tmp
	mv $(LMODULE).lisp.tmp $(LMODULE).lisp
	cat $(LMODULE).tmp $(LMODULE)-clos.lisp > $(LMODULE)-clos.lisp.tmp
	mv $(LMODULE)-clos.lisp.tmp $(LMODULE)-clos.lisp
	rm -f $(LMODULE).tmp
$(LMODULE).asd:
	echo ";; -*- mode: lisp -*-" > $@
	echo "(defsystem ${LMODULE}" >> $@
	echo "  :author \"Toshiaki Maki <makingx@gmail.com>\"" >> $@
	echo "  :depends-on (cffi)" >> $@
	echo "  :version \"${VER}\"" >> $@
	echo "  :components ((:file \"packages\")" >> $@
	echo "	       (:file \"${LMODULE}\" :depends-on (\"packages\"))" >> $@
	echo "	       (:file \"${LMODULE}-clos\" :depends-on (\"${LMODULE}\"))" >> $@
	echo "	       ))" >> $@
asdf: $(TARGET) $(LMODULE).asd
	mkdir $(LMODULE)-$(VER)
	cp $(LMODULE)*.lisp packages.lisp $(LMODULE).asd $(TARGET) $(LMODULE)-$(VER)
	tar czvf $(LMODULE)-$(VER).tar.gz $(LMODULE)-$(VER)/*
	rm -rf $(LMODULE)-$(VER)
install: asdf
	echo "(require :asdf-install) (asdf-install:install \"./${LMODULE}-${VER}.tar.gz\")2" | $(LISP)